package com.zhongyouyun.web.controller;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.zhongyouyun.admin.model.DisplayChildModel;
import com.zhongyouyun.admin.model.DisplayMainModel;
import com.zhongyouyun.admin.model.RoleModel;
import com.zhongyouyun.admin.service.DisplayChildService;
import com.zhongyouyun.admin.service.DisplayMainService;
import com.zhongyouyun.other.model.PageRequest;
import com.zhongyouyun.other.model.Result;
import com.zhongyouyun.utils.QueryUtils;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiResponse;
import io.swagger.annotations.ApiResponses;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Controller
@RequestMapping("/web/display")
@Api(value = "三方登录认证", tags = {"三方登录认证"})
public class DisplayController {

    @Autowired
    private DisplayMainService displayMainService;
    @Autowired
    private DisplayChildService displayChildService;

    @PostMapping("/selectByMain")
    @ResponseBody
    @ApiOperation(value = "获取主表数据", notes = "获取主表数据")
    @ApiResponses({
            @ApiResponse(code=0,message = "操作成功",response = RoleModel.class)
    })
    public Result<List<DisplayMainModel>> selectByMain() {
        List<DisplayMainModel> list=displayMainService.selectList(new QueryWrapper<>());
        List result = new ArrayList();
        for (DisplayMainModel displayMainModel:list) {
            Map map = new HashMap();
            QueryWrapper queryWrapper = new QueryWrapper();
            queryWrapper.eq("main_id",displayMainModel.getId());
            List list1 = displayChildService.selectList(queryWrapper);
            map.put("main",displayMainModel);
            map.put("child",list1);
            result.add(map);
        }
        if(list.size()>0){
            return   Result.<List<DisplayMainModel>>builder().message("成功").status(0).data(result).build();
        }else{
            return Result.<List<DisplayMainModel>>builder().message("没有任何权限").status(1).build();
        }
    }

    @PostMapping("/selectByChild")
    @ResponseBody
    @ApiOperation(value = "查询子表数据", notes = "根据主表id查询子表数据")
    public Result<List<DisplayChildModel>> selectByChild(@RequestBody PageRequest pageRequest) {
        QueryWrapper queryWrapper = new QueryWrapper<>();
        QueryUtils.fiterQuery(queryWrapper, pageRequest);
        List<DisplayChildModel> list=displayChildService.selectList(queryWrapper);
        if(list.size()>0){
            return   Result.<List<DisplayChildModel>>builder().message("成功").status(0).data(list).build();
        }else{
            return Result.<List<DisplayChildModel>>builder().message("没有任何权限").status(1).build();
        }
    }
}
